import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path:'/',
    redirect:'/prior/login'
  },
  {
    path:'/prior',
    name:'前页面',
    component:()=>import('@/views/PriorWindow.vue'),
    children:[
      {
        path:'login',
        name:'登录',
        component:()=>import('@/components/Person/Login.vue')
      },
      {
        path:'regist',
        name:'注册',
        component:()=>import('@/components/Person/Register.vue')
      }
    ]
  },
  {
    path:'/main',
    name:'主页面',
    component:()=>import('@/views/MainWindow.vue'),
    children:[
      {
        path:'home',
        name:'空白',
        component:()=>import('@/components/Home.vue'),
        meta:{
          visiable:true,
          roles:['客户','员工','管理员']
        }
      },
      {
        path:'PersonIndex',
        name:'个人主页',
        children:[
          {
            path:'PersonInfo',
            name:'个人信息',
            component:()=>import('@/components/Person/PersonInfo.vue'),
            meta:{
              visiable:true,
              roles:['客户','员工','管理员']
            }
          },
          {
            path: 'Logout',
            name: '登出',
            redirect: '/prior/login',
            meta:{
              visiable:true,
              roles:['客户','员工','管理员']
            }
          },
        ]
      },
      {
        path:'RoomReview',
        name:'会议室预订',
        component:()=>import('@/components/Client/RoomReview.vue'),
        meta:{
          visiable:true,
          roles:['客户']
        }
      },
      {
        path:'ReviewInfo',
        name:'订单查看',
        component:()=>import('@/components/Client/ReviewInfo.vue'),
        meta:{
          visiable:true,
          roles:['客户']
        }
      },
      {
        path:'CancelReview',
        name:'退订服务',
        component:()=>import('@/components/Client/CancelReview.vue'),
        meta:{
          visiable:true,
          roles:['客户']
        }
      },
      {
        path:'CancelInfo',
        name:'退订查看',
        component:()=>import('@/components/Client/CancelInfo.vue'),
        meta:{
          visiable:true,
          roles:['客户']
        }
      },
      {
        path:'CancelManage',
        name:'退订管理',
        component:()=>import('@/components/Staff/CancelManage.vue'),
        meta:{
          visiable:true,
          roles:['员工']
        },
      },
      {
        path:'RoomCheck',
        name:'会议室维护',
        component:()=>import('@/components/Staff/RoomCheck.vue'),
        meta:{
          visiable:true,
          roles:['员工']
        },
      },
      {
        path:'RoomManage',
        name:'会议室管理',
        component:()=>import('@/components/Admin/RoomManage.vue'),
        meta:{
          visiable:true,
          roles:['管理员']
        }
      },
      {
        path:'UserManage',
        name:'用户管理',
        component:()=>import('@/components/Admin/UserManage.vue'),
        meta:{
          visiable:true,
          roles:['管理员']
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});

// 在router.js导出后添加
// router.beforeEach((to, from, next) => {
//   const userStore = useUserStore()
//   const { userInfo } = storeToRefs(userStore)
//   const requiredRoles = to.meta.roles || [];

//   if (!requiredRoles || requiredRoles.length === 0) {
//     next()//没有设置验证角色
//     return
//   }
//   if (!userInfo.value?.userRole) {
//     next('/user/login')//没有角色
//     return
//   }
//   if (requiredRoles.includes(userInfo.value.userRole)) {
//     next()//角色符合
//   } else {
//     next('/main/home') // 无权限跳转到首页
//   }
// })

export default router


//跳转记得在组件中写
// import {useRouter} from 'vue-router'
// const router=useRouter()//在函数外

// router.push('/user/login')//在路径中加入

//子组件除了children之外，还需设置菜单项el-menu-item的index属性